package com.cszjzx.member.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.cszjzx.member.po.Department;

public class DepartmentDAOImpl implements DepartmentDAO, RowMapper{
    private JdbcHelper helper;
    public DepartmentDAOImpl() throws IOException {
    	helper=new JdbcHelper();
    }
	@Override
	public void add(Department d) throws SQLException {
		String sql="insert into department (name) value(?)";
		Object[]args={d.getName()};
		helper.executeUpdate(sql, args);
		
	}

	@Override
	public void delete(int id) throws SQLException {
		String sql ="delete from department where id=?";
		Object []args={id};
		helper.executeUpdate(sql, args);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> findAll() throws ClassNotFoundException, SQLException, IOException {
		String sql = "select * from department";
		Object []args ={};
		return helper.executeQuery(sql, args, new RowMapper() {
			
			@Override
			public Object mapRow(ResultSet rs) throws SQLException, ClassNotFoundException, IOException {
				Department d =new Department();
				d.setId(rs.getInt("id"));
				d.setName(rs.getString("name"));
				return d;
			}
		});
	}


	@SuppressWarnings("unchecked")
	@Override
	public int getDepartmentId(String department) throws ClassNotFoundException, SQLException, IOException {
		String sql="select id from department where name =?";
		Object [] args={department};
		List<Integer> list=helper.executeQuery(sql, args, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs) throws SQLException, ClassNotFoundException, IOException {
                int x=rs.getInt("id");
				return x;
			}
		});
	    return list.isEmpty()?-1:list.get(0);
	}
	@Override
	public Object mapRow(ResultSet rs) throws SQLException, ClassNotFoundException, IOException {
		// TODO Auto-generated method stub
		return null;
	}
}
